/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.service;

import java.util.ArrayList;
import java.util.List;

import com.thinkgem.jeesite.modules.sys.entity.Area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.service.TreeService;
import com.thinkgem.jeesite.modules.sys.dao.OfficeDao;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 机构Service
 * @author ThinkGem
 * @version 2014-05-16
 */
@Service
@Transactional(readOnly = true)
public class OfficeService extends TreeService<OfficeDao, Office> {

    @Autowired
    private OfficeDao officeDao;

    public Office getVivoOffice(String id) {
        return officeDao.getVivoOffice(id);
    }

    public List<Office> findVivoList(Office o) {
        return officeDao.findVivoList(o);
    }

    public List<Office> findAllVivoList() {
        return officeDao.findAllVivoList();
    }

    public List<String> getPostListByCompany(Office comp) {
        return officeDao.getPostListByCompany(comp);
    }

    public List<Office> getPostListByPID(String pId) {
        return officeDao.getPostListByPID(pId);
    }

    /**
     * 根据职位信息获取部门信息
     * @param post
     * @return
     */
    public Office getVivoSuperDept(Office post) {

        Office dept = post;
        System.out.println(post.getName());
        System.out.println(post.getType());
        System.out.println(post.getId());
        while(dept != null && !dept.getType().equals("2")) {
            // 获取上一级组织机构
            dept = this.getVivoOffice(dept.getParentId());
            System.out.println("获取上级");
            System.out.println(dept.getName());
        }

        System.out.println(post.getName());
        return dept;
    }

    public Office getVivoOfficeByCode(String code, Area area) {
        Office query = new Office();
        query.setCode(code);
        query.setArea(area);
        List<Office> officeList = officeDao.findVivoList(query);

        if (officeList.size() > 0) {
            return officeList.get(0);
        }

        return null;
    }

    @Transactional(readOnly = false)
    public void saveVivo(Office o) {
        if (o.getIsTaskInsert()) {
            o.preInsertByTask();
            officeDao.insertVivo(o);
            return;
        }

        if (o.getIsNewRecord()) {
            o.preInsert();
            officeDao.insertVivo(o);
        } else {
            officeDao.updateVivo(o);
        }
    }

    @Transactional(readOnly = false)
    public void deleteVivo(Office o) {
        officeDao.deleteVivo(o);
    }

    @Transactional(readOnly = false)
    public void deleteAllVivo() {
        officeDao.deleteAllVivo();
    }


	public List<Office> findAll(){
		return UserUtils.getOfficeList();
	}


	public List<Office> findListByTypeAndName(Office o){
		return officeDao.getListByNameAndType(o);
	}

	public List<Office> findList(Boolean isAll){
		if (isAll != null && isAll){
			return UserUtils.getOfficeAllList();
		}else{
			return UserUtils.getOfficeList();
		}
	}


	public List<Office> findAllSecondLevelComp(String pName){
		return officeDao.findAllSecondLevelByPName(pName);
	}

	public List<Office> findMyOwnAllList() {
	    return officeDao.getMyOwnAllList();
    }

    /**
     * 根据职位信息获取部门信息
     * @param post
     * @return
     */
	public Office getSuperDept(Office post) {

        Office dept = post;
        while(dept != null && !dept.getType().equals("2")) {
            // 获取上一级组织机构
            dept = this.get(dept.getParentId());
            System.out.println("获取上级");
            System.out.println(dept.getName());
        }

        return dept;
    }

    /**
     * 根据岗位或者部门信息获取组织机构信息
     * @param post
     * @return
     */
	public Office getSuperComp(Office post) {

        Office comp = post;
        while(comp != null && !comp.getType().equals("1")) {
            // 获取上一级组织机构
            comp = this.get(comp.getParentId());
            System.out.println(comp.getName());
        }

        return comp;
    }

    /**
     * 获取顶级组织机构
     * @param comp
     * @return
     */
    public Office getTopComp(Office comp) {

        Office topComp = comp;
        while(topComp != null && topComp.getParent() != null && !topComp.getParent().getId().equals("0")) {
            // 获取上一级组织机构
            topComp = this.get(topComp.getParent().getId());
            System.out.println(topComp.getName());
        }

        return topComp;
    }


    public Office getOfficeByCode(Office o) {
        List<Office> list = officeDao.getOfficeByCode(o);

        System.out.println(list.size());

        return getSingle(list);
    }

    public Office getCompany(Office comp) {
        List<Office> list = officeDao.getCompany(comp);

        return getSingle(list);
    }

    public Office getVivoCompany(String name) {
        List<Office> list = officeDao.getVivoCompany(name);

        return getSingle(list);
    }

    public List<Office> getCompanyList() {
        return officeDao.getCompanyList();
    }

    public Office getDept(Office dept) {
	    List<Office> list = officeDao.getDept(dept);
	    return getSingle(list);
    }

    public Office getPost(Office post) {
	    List<Office> list = officeDao.getPost(post);
	    return getSingle(list);
    }


    public Office getOffice(Office o) {
	    List<Office> list = officeDao.getOffice(o);
	    return getSingle(list);
    }

    public Office getByCodeLength(Office post) {
	    List<Office> list = officeDao.getListByCodeLength(post);
	    return getSingle(list);
    }



    private Office getSingle(List<Office> list) {
	    if (list == null) {
	        return null;
        }

        if (list.size() > 1) {
	        System.out.println("获取到多个组织机构信息！");
        }

        System.out.println("list size is:" + list.size());

	    if (list.size() > 0) {
            return list.get(0);
        }

        return null;
    }


    /**
     * 根据组织机构获取业绩中心列表
     * @param comp
     * @return
     */
    public List<Office> getYjzxByComp(Office comp) {
        Office yxO = new Office();
        yxO.setParent(comp);
        yxO.setName("营销");
        yxO.setType("2");

        yxO = this.getOffice(yxO);

        if (yxO == null) {
            return null;
        }
//        List<Office> olist = this.find

	    return officeDao.getYjzxByComp(yxO);
    }


    /**
     * 根据职位信息获取部门信息
     * @param post
     * @return
     */
    public Office getSuperCompany(Office post) {
	    Office company = post;

	    while (company != null && !company.getType().equals("1")) {
	        company = this.get(company.getParentId());
        }

        return company;
    }

    public List<Office> getDistinctPostListByCompany(Office comp) {
        return officeDao.getDistinctPostListByCompany(comp);
    }

	
	@Transactional(readOnly = true)
	public List<Office> findList(Office office){
		if(office != null){
			office.setParentIds(office.getParentIds()+"%");
			return dao.findByParentIdsLike(office);
		}
		return  new ArrayList<Office>();
	}
	
	@Transactional(readOnly = false)
	public void save(Office office) {
		super.save(office);
		if (!office.getIsTaskInsert()) {
            UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
        }
	}
	
	@Transactional(readOnly = false)
	public void delete(Office office) {
		super.delete(office);
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}
}
